<br>
<br>
<br>
<input type="hidden" id="saveTradeHTML" />
<div id="tradeDiv">
<?if (isset($_GET['counterTradeId'])) {?>
<?
$html = getCounterHTML($_GET['counterTradeId']);
echo $html;
?>
<?} else {?>
<?
$html = getTradeHTML($loggedInTeam, $allTradesPage, $myTradesPage, $maxPerPage);
echo $html;
?>
<?}?>
</div>

<script>
    var createTradeFormSubmitted = false;
	function submitCreateTradeForm(frm) {
		if (createTradeFormSubmitted) {
			return;
		}
		if (frm.partyTwo.selectedIndex <= 0) {
			alert('Please select a coach!');
		}
		else {
			createTradeFormSubmitted = true;
			frm.submit();
		}
	}
	
	function setItemsInvolvedInTrades(val) {
		itemsInvolvedInTrades = val;
	}
    
    function cancelTrade() {
       createTradeFormSubmitted = false;
       document.forms["cancelForm"].submit();
    }

    function counterTrade(tradeId) {
    	if (location.href.indexOf("counter") != -1) {
    		location.reload();
    	}
    	else {
        	location.href = "../home/homePage.php?counterTradeId=" + tradeId + "#tabs-Trades";
        }
    }

    function confirmTrade(tradeId) {
        location.href = "../trades/confirmTrade.php?tradeId=" + tradeId + "#tabs-Trades";
    }

    function rejectTrade(tradeId) {
        location.href = "../trades/rejectTrade.php?tradeId=" + tradeId + "#tabs-Trades";
    }
    
    function clearTrade() {
       document.getElementById("tradeDiv").innerHTML = document.getElementById("saveTradeHTML").value;
    }
        
    function areItemsInvolvedInTrade() {
    	var items = window.hiddenFrame.itemsInvolvedInTrades;
    	var foundOne = false;
		for (i=0; i< document.getElementById('partyOnePlayers').options.length; i++) {
			if (document.getElementById('partyOnePlayers').options[i].selected) {
				for (j=0; j<items.length; j++) {
					if (items[j].substr(0, 1) != "#" && items[j] == document.getElementById('partyOnePlayers').options[i].value) {
						return true;
					}
				}
			}
		}
		
		for (i=0; i< document.getElementById('partyOnePicks').options.length; i++) {
			if (document.getElementById('partyOnePicks').options[i].selected) {
				for (j=0; j<items.length; j++) {
					if (items[j].substr(0, 1) == "#" && items[j] == document.getElementById('partyOnePicks').options[i].text) {
						return true;
					}
				}
			}
		}
		
		return false;        	
    }

	function tradeTo(frm) {
		if (areItemsInvolvedInTrade()) {
			if (!confirm("You have selected at least one player or pick that is already involved in a PROPOSED trade. Are you sure you want to include it in another trade?")) {
				return;
			}
		}
		
		for (i=0; i< document.getElementById('partyOnePlayers').options.length; i++) {
			if (document.getElementById('partyOnePlayers').options[i].selected) {
				document.getElementById('partyTwoGets').innerHTML += document.getElementById('partyOnePlayers').options[i].text + '<br>';
				frm.elements['partyTwoReceives'].value += document.getElementById('partyOnePlayers').options[i].value + ",";
				document.getElementById('partyOnePlayers').options[i] = null;
			}
		}
		
		for (i=0; i< document.getElementById('partyOnePicks').options.length; i++) {
			if (document.getElementById('partyOnePicks').options[i].selected) {
				document.getElementById('partyTwoGets').innerHTML += "Pick " + document.getElementById('partyOnePicks').options[i].text + '<br>';
				frm.elements['partyTwoReceives'].value += "#" + document.getElementById('partyOnePicks').options[i].value + ",";
				document.getElementById('partyOnePicks').options[i] = null;
			}
		}
	}
	
function tradeFor(frm) {
		for (i=0; i< document.getElementById('partyTwoPlayers').options.length; i++) {
			if (document.getElementById('partyTwoPlayers').options[i].selected) {
				document.getElementById('partyOneGets').innerHTML += document.getElementById('partyTwoPlayers').options[i].text + '<br>';
				frm.elements['partyOneReceives'].value += document.getElementById('partyTwoPlayers').options[i].value + ",";
				document.getElementById('partyTwoPlayers').options[i] = null;
			}
		}
		
		for (i=0; i< document.getElementById('partyTwoPicks').options.length; i++) {
			if (document.getElementById('partyTwoPicks').options[i].selected) {
				document.getElementById('partyOneGets').innerHTML += "Pick " + document.getElementById('partyTwoPicks').options[i].text + '<br>';
				frm.elements['partyOneReceives'].value += "#" + document.getElementById('partyTwoPicks').options[i].value + ",";
				document.getElementById('partyTwoPicks').options[i] = null;
			}
		}
	}

	function tradeToCounter(frm) {
		for (i=0; i< document.getElementById('partyOnePlayers').options.length; i++) {
			if (document.getElementById('partyOnePlayers').options[i].selected) {
				document.getElementById('partyTwoReceives').options[document.getElementById('partyTwoReceives').length - 1] = new Option(document.getElementById('partyOnePlayers').options[i].text, document.getElementById('partyOnePlayers').options[i].value);
				document.getElementById('partyOnePlayers').options[i] = null;
			}
		}
		
		for (i=0; i< document.getElementById('partyOnePicks').options.length; i++) {
			if (document.getElementById('partyOnePicks').options[i].selected) {
				document.getElementById('partyTwoReceives').options[document.getElementById('partyTwoReceives').length - 1] = new Option("Pick " + document.getElementById('partyOnePicks').options[i].text, "#" + document.getElementById('partyOnePicks').options[i].value);
				document.getElementById('partyOnePicks').options[i] = null;
			}
		}
	}

	function removeTradeFor(frm) {
		for (i=0; i< document.getElementById('partyOneReceives').options.length; i++) {
			if (document.getElementById('partyOneReceives').options[i].selected) {
                           if (document.getElementById('partyOneReceives').options[i].text.substr(0, 1) == "#") {
				document.getElementById('partyTwoPicks').options[document.getElementById('partyTwoPicks').length - 1] = new Option(document.getElementById('partyOneReceives').options[i].text, document.getElementById('partyOneReceives').options[i].value.substr(1));
				document.getElementById('partyOneReceives').options[i] = null;
                           }
                           else {
				document.getElementById('partyTwoPlayers').options[document.getElementById('partyTwoPlayers').length - 1] = new Option(document.getElementById('partyOneReceives').options[i].text, document.getElementById('partyOneReceives').options[i].value);
				document.getElementById('partyOneReceives').options[i] = null;
                           }
			}
		}
	}

	function removeTradeTo(frm) {
		for (i=0; i< document.getElementById('partyTwoReceives').options.length; i++) {
			if (document.getElementById('partyTwoReceives').options[i].selected) {
                           if (document.getElementById('partyTwoReceives').options[i].text.substr(0, 1) == "#") {
				document.getElementById('partyOnePicks').options[document.getElementById('partyOnePicks').length - 1] = new Option(document.getElementById('partyTwoReceives').options[i].text, document.getElementById('partyTwoReceives').options[i].value.substr(1));
				document.getElementById('partyTwoReceives').options[i] = null;
                           }
                           else {
				document.getElementById('partyOnePlayers').options[document.getElementById('partyOnePlayers').length - 1] = new Option(document.getElementById('partyTwoReceives').options[i].text, document.getElementById('partyTwoReceives').options[i].value);
				document.getElementById('partyTwoReceives').options[i] = null;
                           }
			}
		}
	}
	
function tradeForCounter(frm) {
		for (i=0; i< document.getElementById('partyTwoPlayers').options.length; i++) {
			if (document.getElementById('partyTwoPlayers').options[i].selected) {
				document.getElementById('partyOneReceives').options[document.getElementById('partyOneReceives').length - 1] = new Option(document.getElementById('partyTwoPlayers').options[i].text, document.getElementById('partyTwoPlayers').options[i].value);
				document.getElementById('partyTwoPlayers').options[i] = null;
			}
		}
		
		for (i=0; i< document.getElementById('partyTwoPicks').options.length; i++) {
			if (document.getElementById('partyTwoPicks').options[i].selected) {
				document.getElementById('partyOneReceives').options[document.getElementById('partyOneReceives').length - 1] = new Option("Pick " + document.getElementById('partyTwoPicks').options[i].text, "#" + document.getElementById('partyTwoPicks').options[i].value);
				document.getElementById('partyTwoPicks').options[i] = null;
			}
		}
	}
	
var tradeFormSubmitted = false;
function validateTrade(frm) {
	if (tradeFormSubmitted) {
		return;
	}
	if (frm.partyTwoReceives.value == null || frm.partyTwoReceives.value == '' || frm.partyOneReceives.value == null || frm.partyOneReceives.value == '') {
		alert("Both parties need to receive an item.");
		return;
	}
	
	tradeFormSubmitted = true;
	frm.submit();	
}
function validateCounter(frm) {
	if (frm.partyTwoReceives.options.length == 0 || frm.partyOneReceives.options.length == 0) {
		alert("Both parties need to receive an item.");
		return;
	}

        for (i=0; i< frm.partyOneReceives.options.length; i++) {
           if (frm.partyOneReceives.options[i].value != "") {
              frm.partyOneReceives.options[i].selected = true;
           }
        }

        for (i=0; i< frm.partyTwoReceives.options.length; i++) {
           if (frm.partyTwoReceives.options[i].value != "") {
              frm.partyTwoReceives.options[i].selected = true;
           }
        }
	
	frm.submit();	
}

document.getElementById('saveTradeHTML').value = "<?echo $html?>";
</script>